/*
 * 保存销售大区交易
 * 参数   交易码：trancode=15113
 * 
 */

package org.ccams.tran;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.ccams.domain.TZone;
import org.json.JSONArray;
/**
 * 保存销售大区交易
 * @author liuguangshuai@gmail.com
 * @系统名称：定制化辅助生成制造系统
 * @时间：Aug 17, 2008
 */
public class Tran15113 extends AbstractJsonTran {

	@SuppressWarnings("unchecked")
	public void buildJSONObject(Map model, JSONObject json,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String params = (String) request.getParameter("SaleArea");
		JSONArray saleAreas = new JSONArray(params);
		HashMap<String, Object> jsonMap = new HashMap<String, Object>();
		boolean success = false;
		String message = "";
		JSONArray newSaleAreas = new JSONArray();
		int k = 0;
		for(int i=0;i<saleAreas.length();i++){
			org.json.JSONObject saleArea = saleAreas.optJSONObject(i);
			if(saleArea.has("regionId")){
				TZone t = new TZone();
				t.setValid(0);
				t.setZoName(saleArea.getString("zoName"));
				t.setZoneNo(saleArea.getString("zoneNo"));
				t.setRegionId(saleArea.getLong("regionId"));
				k = getCcamsService().getTzoneDao().updateTZone(t);	//更新			
			}else{
				newSaleAreas.put(saleArea);
			}			
		}
		for(int i=0;i<newSaleAreas.length();i++){
			org.json.JSONObject newsaleArea = saleAreas.optJSONObject(i);
			TZone t = new TZone();
			t.setValid(0);
			t.setZoName(newsaleArea.getString("zoName"));
			t.setZoneNo(newsaleArea.getString("zoneNo"));
			k = getCcamsService().getTzoneDao().insertTZone(t);
		}
		if (k == 1) {
			success = true;
			message = "大区保存成功";
		} else {
			success = false;
			message = "大区保存失败";
		}
		jsonMap.put("success", success);
		jsonMap.put("message", message);
		json.putAll(jsonMap);
	}
}
